package snooker.scoreboard.activity;

import java.util.List;

import snooker.scoreboard.Ball;
import snooker.scoreboard.FrameState;
import snooker.scoreboard.Match;
import snooker.scoreboard.Type;
import snooker.scoreboard.event.FinishActivityTouchListener;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;
import android.widget.TableLayout.LayoutParams;

public class FrameHistoryActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.history);
		TableLayout tl = (TableLayout)findViewById(R.id.historyTableLayout);
		findViewById(R.id.historyScrollView).setOnTouchListener(
			new FinishActivityTouchListener(this, 
					FinishActivityTouchListener.DIRECTION_TO_RIGHT));
		Match match = Match.getInstance();
		Bundle extras = getIntent().getExtras();
		int frameId = -1;
		if (extras != null) {
			frameId = getIntent().getExtras().getInt("frameid");
		}
		List<FrameState> fsl = null;
		if (frameId == -1) {
			fsl = match.getActFrame().getHistory().getHistory();
		} else {  
			fsl = match.getFrames().get(frameId).getHistory().getHistory();
		}
		int breakId = 0;
		boolean showPlayerName = true;
		for (FrameState fs: fsl) {
			if (breakId == 0 || breakId != fs.getBreakId()) {
				showPlayerName = true;
			} else {
				showPlayerName = false;
			}
			TableRow row = new TableRow(this);
			TextView nameV = new TextView(this);
			if (showPlayerName) {
				if (Match.PLAYER1 == fs.getPlayerId()) {
					nameV.setText(match.getPlayer1Name());
				} else {
					nameV.setText(match.getPlayer2Name());
				}
			}
			LinearLayout mLinearLayout = new LinearLayout(this);
			ImageView i = new ImageView(this);
			i.setImageResource(fs.getBall().getResourceId());
			i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
			i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			mLinearLayout.addView(i);
				TextView breakV = new TextView(this);
			breakV.setText(fs.getBreakId() == 0 ? "" : Integer.toString(fs.getBreakId()));
			TextView typeV = new TextView(this);
			typeV.setText(Type.Pot.equals(fs.getType()) ? "" : fs.getType().toString());
			TextView timeV = new TextView(this);
			timeV.setText(fs.getEventTimeString());
			row.addView(nameV);
			row.addView(getBallImage(fs.getBall()));
			row.addView(typeV);
			row.addView(timeV);
			tl.addView(row);
			breakId = fs.getBreakId();
		}
	}
	
	private ImageView getBallImage(Ball ball) {
		Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
				ball.getResourceId());
		int width = bitmapOrg.getWidth(); 
		int height = bitmapOrg.getHeight(); 
		int newWidth = 20; 
		int newHeight = 20; 
		float scaleWidth = ((float) newWidth) / width; 
		float scaleHeight = ((float) newHeight) / height; 
		Matrix matrix = new Matrix();
		// resize the bit map
		matrix.postScale(scaleWidth, scaleHeight);

		// recreate the new Bitmap
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); 
		ImageView imageView = new ImageView(this);
		BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 
		// set the Drawable on the ImageView
		imageView.setImageDrawable(bmd);
		// center the Image
		imageView.setScaleType(ScaleType.FIT_START);
		return imageView;
	}
	
}
